import _LocaleProvider from "choerodon-ui/lib/locale-provider";
import _localeContext from "choerodon-ui/pro/lib/locale-context";
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _createSuper from "@babel/runtime/helpers/esm/createSuper";

var _dec, _class;

import React, { Component } from 'react';
import { LocaleProvider } from 'antd';
import { connect } from 'dva';
import { resolveRequire } from "../utils";
var LocalProviderAsync = (_dec = connect(function (_ref) {
  var _ref$global = _ref.global,
      global = _ref$global === void 0 ? {} : _ref$global;
  return {
    language: global.language,
    cpaasUILocale: global.cpaasUILocale,
    c7nLocale: global.c7nLocale,
    supportLanguage: global.supportLanguage
  };
}), _dec(_class = /*#__PURE__*/function (_Component) {
  _inherits(LocalProviderAsync, _Component);

  var _super = _createSuper(LocalProviderAsync);

  function LocalProviderAsync() {
    _classCallCheck(this, LocalProviderAsync);

    return _super.apply(this, arguments);
  }

  _createClass(LocalProviderAsync, [{
    key: "componentDidUpdate",
    value: function componentDidUpdate(prevProps) {
      var _this$props = this.props,
          supportLanguage = _this$props.supportLanguage,
          language = _this$props.language;

      if (prevProps.supportLanguage !== supportLanguage) {
        var c7nLocaleContext = {};

        if (supportLanguage) {
          supportLanguage.forEach(function (intlRecord) {
            c7nLocaleContext[intlRecord.code] = intlRecord.name;
          });
        } // 更新 c7n 的 locale supports


        _localeContext.setSupports(c7nLocaleContext);
      }

      if (prevProps.language !== language && language) {// c7n localeContext already update in models/global; so not need updatet there
        // localeContext.setLocale(language); // this is error, locale should be a deep object with intl;
      }
    }
  }, {
    key: "render",
    value: function render() {
      var _this$props2 = this.props,
          cpaasUILocale = _this$props2.cpaasUILocale,
          c7nLocale = _this$props2.c7nLocale;
      return /*#__PURE__*/React.createElement(_LocaleProvider, {
        locale: resolveRequire(c7nLocale)
      }, /*#__PURE__*/React.createElement(LocaleProvider, Object.assign({}, this.props, {
        locale: resolveRequire(cpaasUILocale)
      })));
    }
  }]);

  return LocalProviderAsync;
}(Component)) || _class);
export { LocalProviderAsync as default };